<?php
class cache_helper{
    private $base;
    public function __construct(){
        $this->base=rtrim(P_CACHE,'/');
    }
    public function set_dir($dir){
        $this->base=rtrim($dir,'/');
    }
    public function cache_read($file, $dir = '', $mode = '') {
        $file = $dir ? $this->base.'/'.$dir.'/'.$file : $this->base.'/'.$file;
        if(!is_file($file)) return $mode ? '' : array();
        return $mode ? file_get_contents($file) : include $file;
    }
    
    public function cache_write($file, $string, $dir = '') {
        if(is_array($string)) $string = "<?php defined('A') or exit(''); return ".strip_nr(var_export($string, true)).";";
        $file = $dir ? $this->base.'/'.$dir.'/'.$file : $this->base.'/'.$file;
        return file_put_contents($file, $string);
    }

    public function cache_delete($file, $dir = '') {
        $file = $dir ? $this->base.'/'.$dir.'/'.$file : $this->base.'/'.$file;
        return is_file($file) ? unlink($file) : false;
    }
    //
}